package com.swlz.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.swlz.commons.DBUtil;
import com.swlz.entity.Fiction;

import java.util.Date;
/**
 * <pre>
 * 数据访问对象
 * 小说
 * TableName : FICTION
 * ClassName : com.swlz.dao.FictionDAO
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-07-01 17:25:46
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class FictionDAO {

	/**
	 * 添加一个小说
	 * 包含所有字段
	 * @param fiction - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insert(Fiction fiction) throws SQLException {
		if(fiction.getFictionId()==null) {
			fiction.setFictionId(DBUtil.nextId());
		}
		return DBUtil.insert(fiction);
	}
 
	
	/**
	 * 添加一个小说
	 * 只包含非null的字段
	 * @param fiction - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insertSelective(Fiction fiction) throws SQLException {
		if(fiction.getFictionId()==null) {
			fiction.setFictionId(DBUtil.nextId());
		}
		return DBUtil.insertSelective(fiction);
	}
	

	/**
	 * 根据主键更新一个小说
	 * 包含所有字段
	 * @param fiction - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int update(Fiction fiction) throws SQLException {
		return DBUtil.update(fiction);
	}
	
	/**
	 * 根据主键更新一个小说
	 * 只包含非null的字段
	 * @param fiction - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int updateSelective(Fiction fiction) throws SQLException {
		return DBUtil.updateSelective(fiction);
	}
	
	/**
	 * 根据主键删除一个小说
	 * @param fictionId - 主键
	 * @return
	 * @throws SQLException
	 */
	public int deleteById(Long fictionId) throws SQLException  {
		return DBUtil.execute("DELETE FROM FICTION WHERE FICTION_ID=?", fictionId);
	}
	
	/**
	 * 根据主键获取一个小说
	 * @param fictionId - 主键
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public Fiction selectById(Long fictionId) throws SQLException  {
		return DBUtil.get("SELECT * FROM FICTION WHERE FICTION_ID=?", Fiction.class ,fictionId);
	}

		
	/**
	 * 根据模型中的非空字段查询
	 * @param fictionId - 实体对象
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public Fiction selectByModel(Fiction fiction)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM FICTION WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(fiction,sql,params);
		return DBUtil.get(sql.toString(), Fiction.class, params.toArray());
	}
	
	/**
	 * 根据模型中的非空字段查询
	 * @param fictionId - 实体对象
	 * @param index - 开始索引
	 * @param len - 返回长度
	 * @return
	 * @throws SQLException
	 */
	public List<Fiction> selectByModel(Fiction fiction,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM FICTION WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(fiction,sql,params);
		sql.append(" ORDER BY FICTION_ID DESC LIMIT ?,?");
		params.add(index);
		params.add(len);
		return DBUtil.list(sql.toString(), Fiction.class, params.toArray());
	}

	/**
	 * 根据模型中的非空字段统计
	 * @param fictionId - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long countByModel(Fiction fiction)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM FICTION WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(fiction,sql,params);
		return DBUtil.getLong(sql.toString(), params.toArray());
	}
	
	/**
	 * 格式化where条件和参数
	 * @param fictionId - 实体对象
	 * @param sql - SQL语句
	 * @param params - SQL语句中的参数
	 */
	private void where(Fiction fiction,StringBuffer sql,List<Object> params) {
		if(fiction==null){
			return;
		}

		if(fiction.getFictionId()!=null) {
			sql.append(" AND FICTION_ID=?");
			params.add(fiction.getFictionId());
		}

		if(fiction.getUserId()!=null) {
			sql.append(" AND USER_ID=?");
			params.add(fiction.getUserId());
		}

		if(fiction.getFictionTypeId()!=null) {
			sql.append(" AND FICTION_TYPE_ID=?");
			params.add(fiction.getFictionTypeId());
		}

		if(fiction.getFictionName()!=null) {
			sql.append(" AND FICTION_NAME=?");
			params.add(fiction.getFictionName());
		}

		if(fiction.getFictionTime()!=null) {
			sql.append(" AND FICTION_TIME=?");
			params.add(fiction.getFictionTime());
		}

		if(fiction.getFictionTags()!=null) {
			sql.append(" AND FICTION_TAGS=?");
			params.add(fiction.getFictionTags());
		}

		if(fiction.getFictionImg()!=null) {
			sql.append(" AND FICTION_IMG=?");
			params.add(fiction.getFictionImg());
		}

		if(fiction.getFictionMemo()!=null) {
			sql.append(" AND FICTION_MEMO=?");
			params.add(fiction.getFictionMemo());
		}

		if(fiction.getFictionSectionNum()!=null) {
			sql.append(" AND FICTION_SECTION_NUM=?");
			params.add(fiction.getFictionSectionNum());
		}

		if(fiction.getFictionNum()!=null) {
			sql.append(" AND FICTION_NUM=?");
			params.add(fiction.getFictionNum());
		}

		if(fiction.getFictionFavoriteNum()!=null) {
			sql.append(" AND FICTION_FAVORITE_NUM=?");
			params.add(fiction.getFictionFavoriteNum());
		}

		if(fiction.getFictionReadNum()!=null) {
			sql.append(" AND FICTION_READ_NUM=?");
			params.add(fiction.getFictionReadNum());
		}

		if(fiction.getFictionScore()!=null) {
			sql.append(" AND FICTION_SCORE=?");
			params.add(fiction.getFictionScore());
		}

		if(fiction.getFictionLastTime()!=null) {
			sql.append(" AND FICTION_LAST_TIME=?");
			params.add(fiction.getFictionLastTime());
		}

		if(fiction.getFictionStatus()!=null) {
			sql.append(" AND FICTION_STATUS=?");
			params.add(fiction.getFictionStatus());
		}

	}
}